<?php
namespace App\Http\Controllers\Admin;
use App\Http\Controllers\AdminController;
use App\Models\Service as TService;
use Illuminate\Http\Request;
use App\Models\Level;

class Service extends AdminController{
    
    public function index(Request $req){
        $psize = 15;
        if($req->method() == 'POST'){
            $list = TService::orderBy('id')->paginate($psize);
            foreach ($list as $v) {
                $v->url = getImage($v->url);
            }
            wxresp(1, ['rows' => $list->items(), 'total' => $list->total()]);
        }
        return $this->V("index");
    }
    
    public function del(Request $req){
        if ($req->method() == 'POST') {
            $id = $req->input('id', 0);
            $item = TService::find($id);
            if (!$item) wxresp(0, '客服不存在');
            TService::where('id', $id)->delete();
            wxresp(1);
        }
        wxresp(0);
    }
    
    public function post(Request $req){
        $id = $req->input("sid",0);
        $data = [
                'title' => $req->post('title'),
                'url' => uploadImage($req->post('url')),
                'starttime' => $req->post('starttime'),
                'endtime' => $req->post('endtime'),
        ];
        if ($id){
            TService::where('id', $id)->update($data);
        }else{
            $id = TService::insertGetId($data);
        }
        wxresp(1);
    }
}